<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="inputListForm" :model="InputListFormData" :rules="rules" size="mini" label-width="100px">
        <el-col :span="12">
          <el-form-item label="进货单ID" prop="inputListId">
            <el-input v-model="InputListFormData.inputListId" placeholder="请输入进货单ID" readonly :disabled='true'
              clearable :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品" prop="goodsId">
            <el-select v-model="InputListFormData.goodsId" placeholder="请选择商品" filterable
              :style="{width: '100%'}" @change="setGoods">
              <el-option v-for="(item, index) in goodsIdOptions" :key="index" :label="item.goodsName"
                :value="item.goodsId" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品数量" prop="goodsNumber">
            <el-input v-model="InputListFormData.goodsNumber" placeholder="请输入商品数量" clearable
              :style="{width: '100%'}" @change="setTotalCost"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否已执行" prop="executed">
            <el-select v-model="InputListFormData.executed" :disabled="true" placeholder="请选择是否已执行" :style="{width: '100%'}">
              <el-option v-for="(item, index) in executedOptions" :key="index" :label="item.label"
                :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="总金额" prop="goodsTotalCost">
            <el-input v-model="InputListFormData.goodsTotalCost" placeholder="请输入总金额" readonly
              :disabled='true' clearable :style="{width: '100%'}" @change="setTotalCost"></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button type="success" @click="doIt">执行</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import {inputListUpdate,goodsSearch,inputListExecute} from '@/api/storage'
export default {
  components: {},
  props: ['inputListUpdateData', 'goodsList'],
  data() {
    return {
      InputListFormData: {
        inputListId: undefined,
        goodsId: undefined,
        goodsNumber: undefined,
        executed: 0,
        goodsTotalCost: undefined,
      },
      rules: {
        inputListId: [{
          required: true,
          message: '请输入进货单ID',
          trigger: 'blur'
        }],
        goodsId: [{
          required: true,
          message: '请选择商品',
          trigger: 'change'
        }],
        goodsNumber: [{
          required: true,
          message: '请输入商品数量',
          trigger: 'blur'
        }],
        executed: [{
          required: true,
          message: '请选择是否已执行',
          trigger: 'change'
        }],
        goodsTotalCost: [{
          required: true,
          message: '请输入总金额',
          trigger: 'blur'
        }],
      },
      goodsIdOptions: [],
      executedOptions: [{
        "label": "未执行",
        "value": 0
      }, {
        "label": "已执行",
        "value": 1
      }],
      goods: {}
    }
  },
  computed: {},
  watch: {
    inputListUpdateData: function(n, o){
      this.InputListFormData = n
    }
  },
  created() {
    this.goodsIdOptions = this.goodsList
    this.InputListFormData = this.inputListUpdateData
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['inputListForm'].validate(valid => {
        if (!valid) return
        inputListUpdate(this.InputListFormData)
      })
    },
    doIt(){
      inputListExecute({inputListId: this.InputListFormData.inputListId})
    },
    resetForm() {
      this.$refs['inputListForm'].resetFields()
    },
    // 当前选定商品
    setGoods(value){
      goodsSearch({goodsId: value}).then(res => {
        this.goods = res.data.goods
      })
    },
    // 计算总金额
    setTotalCost(value){
      this.InputListFormData.goodsTotalCost = value * this.goods.goodsValue
    },
  }
}

</script>
<style>
</style>
